package com.bzxhh;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test01 {

    public static void main(String[] args) {
//        String sql = "UPDATE \n" +
//                "\tTABLE_TMP_YSZXQKFX \n" +
//                "SET \n" +
//                "\tF_NDYSZB = (\n" +
//                "\t\tSELECT F_NDYSZB\n" +
//                "\t\tFROM \n" +
//                "\t\t\t(SELECT \n" +
//                "\t\t\t\tje.F_BH,SUM(je.F_YSJE) AS F_NDYSZB \n" +
//                "\t\t\tFROM \n" +
//                "\t\t\t\tPMAS.BFP_DCT_YSBZFZ ysbz LEFT JOIN\n" +
//                "\t\t\t\t(SELECT\n" +
//                "\t\t\t\t\tA.SJKJ_XMBH3 AS F_BH,A.DATE_YEAR AS date_year,A.ZBSJ_DATA2 AS F_YSJE,A.DWZD_BH,A.SJKJ_XMBH2 AS F_YWBM,A.YSBB_BH\n" +
//                "\t\t\t\tFROM\n" +
//                "\t\t\t\t\tpmas.KWNBY_0013_SJKJ0013 A ,\n" +
//                "\t\t\t\t\tPMAS.BFP_DCT_YSBZFZ B\n" +
//                "\t\t\t\tWHERE\n" +
//                "\t\t\t\t\tA.SJKJ_XMBH3 = B.F_BH\n" +
//                "\t\t\t\t\tAND A.DATE_DATE = B.DATE_YEAR\n" +
//                "\t\t\t\t\tAND A.DWZD_BH = B.DWZD_BH\n" +
//                "\t\t\t\t\tAND A.SJKJ_XMBH1 = '0000'\n" +
//                "\t\t\t\t\tAND A.TJZB_BH = '004'\n" +
//                "\t\t\t\t\tAND A.SJKJ_XMBH4 = '0006'\n" +
//                "\t\t\t\t\tAND A.SJKJ_XMBH2 NOT IN ('#ROOT',\n" +
//                "\t\t\t\t\t'9009')\n" +
//                "\t\t\t\t) je ON (je.date_year = ysbz.date_year AND je.dwzd_bh = ysbz.dwzd_bh AND je.f_bh = ysbz.f_bh)\n" +
//                "\t\t\tWHERE \n" +
//                "\t\t\t\tje.date_year = '2022' AND je.dwzd_bh = '01006' AND ysbz.f_mx = '1'\n" +
//                "\t\t\t\tAND je.ysbb_bh IN (\n" +
//                "\t\t\t\t\tSELECT\n" +
//                "\t\t\t\t\t\tzd_bh\n" +
//                "\t\t\t\t\tFROM\n" +
//                "\t\t\t\t\t\tsys_ysver\n" +
//                "\t\t\t\t\tWHERE\n" +
//                "\t\t\t\t\t\tf_year = '2022'\n" +
//                "\t\t\t\t\t\tAND zd_char1 = '1'\n" +
//                "\t\t\t\t\t)\n" +
//                "\t\t\tGROUP BY je.F_BH ) ysje\n" +
//                "\t\tWHERE F_YSBZFZBH = ysje.F_BH\n" +
//                "\t)";
        String sql = "INSERT\n" +
                "\tINTO\n" +
                "\tTABLE_TMP_YSZXQKFX (F_YSBZFZBH,\n" +
                "\tF_YSBZFZ,\n" +
                "\tF_JS)\n" +
                "SELECT\n" +
                "\tF_BH AS F_YSBZFZBH,\n" +
                "\tF_MC AS F_YSBZFZ ,\n" +
                "\tF_JS\n" +
                "FROM\n" +
                "\tpmas.bfp_dct_ysbzfz\n" +
                "WHERE\n" +
                "\tf_used = '1'\n" +
                "\tAND DWZD_BH <= ('01005')\n" +
                "\tAND date_year = '2022'\n" +
                "ORDER BY\n" +
                "\tF_BH";
        sql = sql.toUpperCase();
        String tj = "DWZD_BH <= ('01007')"; //替换内容
//        String tj = "DWZD_BH = '01007'"; //替换内容
        tj = tj.toUpperCase();
        String tjArr[] = tj.split(" ");
        String tjArrSecond = tjArr[1];
        String ttjArrSecondFirstChar = tjArrSecond.substring(0, 1);
        String tjArrLast = tjArr[tjArr.length - 1];
        String tjArrLastFirstChar = tjArrLast.substring(0, 1);
        String tjArrLastLastChar = tjArrLast.substring(tjArrLast.length() - 1, tjArrLast.length());

//        String regex = "DWZD_BH.*?\\(.*?\\)"; //正则
//        String regex = "DWZD_BH.*?\\'.*?\\'"; //正则
//        String regex = "DWZD_BH.*?=.*?\\'.*?\\'"; //正则
        String regex = tjArr[0] + ".*?" + ttjArrSecondFirstChar + ".*?" + "\\" + tjArrLastFirstChar + ".*?" + "\\" + tjArrLastLastChar; //正则
        System.out.println(sql);
        System.out.println(sql.length());
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sql);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sql);
        if (matcher.find()) {
            System.out.println(matcher.start(0));
            System.out.println(matcher.end(0));
            stringBuffer.replace(matcher.start(0), matcher.end(0), tj);
        }
        System.out.println(stringBuffer);
    }
}
